<?php

namespace app\admin\controller;

use tian\Controller;
use tian\Request;
use tian\Session;
use think\facade\Db;

class Index extends Controller
{
    public function index()
    {
        $this->fetch("index");
    }
    
    public function login()
    {
        $username = input("username");
        $password = input("password");
        
        if($username == ''){
            returnJson(0,'用户名不能为空');
        }
        
        if($password == ''){
            returnJson(0,'密码不能为空');
        }
        
        $infoAdmin = Db::name('admin')->where('username',$username)->find();
        if(empty($infoAdmin)){
            returnJson(0,'用户名不存在');
        }
        
        if($infoAdmin['status'] != 1){
            returnJson(0,'账号被冻结');
        }
        
        $passwordStr = md5(md5($password) . $infoAdmin['id'] . $infoAdmin['addtime']);
        if($infoAdmin['password'] != $passwordStr){
            returnJson(0,'密码错误');
        }
        
        $temp = [
            'logintime' => time()
        ];
        Db::name('admin')->where('id',$infoAdmin['id'])->update($temp);
        
        Session::set('id',$infoAdmin['id']);
        
        //echo"<pre>";var_dump($infoAdmin);die;
        
        //redirect(url('home/index'));
        returnJson(1,'登录成功',['url'=>url('/admin/home/index')]);
    }
	
	public function outLogin()
    {
        Session::delete('id');
        Session::clear();

        returnJson(2,'退出成功',['url'=>url('/admin/index/index')]);
    }
    
}
